package com.distribute.framework.core.util;

import java.util.ArrayList;

import com.distribute.framework.core.web.helper.Utils;

/**
 * 坐标转换
 * 一个提供了百度坐标（BD09）、国测局坐标（火星坐标，GCJ02）、和WGS84坐标系之间的转换的工具类
 * Created by wangxin on 2017/5/22.
 */
public class CoordTransformUtil {

    private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    private static double PI = 3.1415926535897932384626;
    private static double a = 6378245.0;
    private static double ee = 0.00669342162296594323;

    /**
     * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
     * 即 百度 转 谷歌、高德
     * @param bd_lon
     * @param bd_lat
     * @return Double[lon,lat]
     */
    public static Double[] BD09ToGCJ02(Double bd_lon,Double bd_lat){
        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
        Double[] arr = new Double[2];
        arr[0] = z * Math.cos(theta);
        arr[1] = z * Math.sin(theta);
        return arr;
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
     * 即谷歌、高德 转 百度
     * @param gcj_lon
     * @param gcj_lat
     * @return Double[lon,lat]
     */
    public static Double[] GCJ02ToBD09(Double gcj_lon,Double gcj_lat){
        double z = Math.sqrt(gcj_lon * gcj_lon + gcj_lat * gcj_lat) + 0.00002 * Math.sin(gcj_lat * x_PI);
        double theta = Math.atan2(gcj_lat, gcj_lon) + 0.000003 * Math.cos(gcj_lon * x_PI);
        Double[] arr = new Double[2];
        arr[0] = z * Math.cos(theta) + 0.0065;
        arr[1] = z * Math.sin(theta) + 0.006;
        return arr;
    }

    
    public static Double[] WGS84ToGCJ02(String wgs_lon,String wgs_lat){
    	return WGS84ToGCJ02(Double.parseDouble(wgs_lon),Double.parseDouble(wgs_lat));
    }
    
    /**
     * WGS84转GCJ02
     * @param wgs_lon
     * @param wgs_lat
     * @return Double[lon,lat]
     */
    public static Double[] WGS84ToGCJ02(Double wgs_lon,Double wgs_lat){
        if(outOfChina(wgs_lon, wgs_lat)){
            return new Double[]{wgs_lon,wgs_lat};
        }
        double dlat = transformlat(wgs_lon - 105.0, wgs_lat - 35.0);
        double dlng = transformlng(wgs_lon - 105.0, wgs_lat - 35.0);
        double radlat = wgs_lat / 180.0 * PI;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        Double[] arr = new Double[2];
        arr[0] = wgs_lon + dlng;
        arr[1] = wgs_lat + dlat;
        return arr;
    }

    /**
     * GCJ02转WGS84
     * @param gcj_lon
     * @param gcj_lat
     * @return Double[lon,lat]
     */
    public static Double[] GCJ02ToWGS84(Double gcj_lon,Double gcj_lat){
        if(outOfChina(gcj_lon, gcj_lat)){
            return new Double[]{gcj_lon,gcj_lat};
        }
        double dlat = transformlat(gcj_lon - 105.0, gcj_lat - 35.0);
        double dlng = transformlng(gcj_lon - 105.0, gcj_lat - 35.0);
        double radlat = gcj_lat / 180.0 * PI;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        double mglat = gcj_lat + dlat;
        double mglng = gcj_lon + dlng;
        return new Double[]{gcj_lon * 2 - mglng, gcj_lat * 2 - mglat};
    }

    private static Double transformlat(double lng, double lat) {
        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static Double transformlng(double lng,double lat) {
        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * outOfChina
     * @描述: 判断是否在国内，不在国内则不做偏移
     * @param lng
     * @param lat
     * @return {boolean}
     */
    private static boolean outOfChina(Double lng,Double lat) {
        return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
    };
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
private static double EARTH_RADIUS = 6378.137;  
    
    private static double rad(double d) {  
        return d * Math.PI / 180.0;  
    }  
  
    /** 
     * 通过经纬度获取距离(单位：米) 
     * @param lat1 
     * @param lng1 
     * @param lat2 
     * @param lng2 
     * @return 
     */  
    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {  
        double radLat1 = rad(lat1);  
        double radLat2 = rad(lat2);  
        double a = radLat1 - radLat2;  
        double b = rad(lng1) - rad(lng2);  
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)  
                + Math.cos(radLat1) * Math.cos(radLat2)  
                * Math.pow(Math.sin(b / 2), 2)));  
        s = s * EARTH_RADIUS;  
        s = Math.round(s * 10000d) / 10000d;  
        s = s*1000;  
        return s;  
    }  
	
	public static Boolean ptIn(Double lng,Double lat,String ranges){
		ArrayList<Double> polygonXA = new ArrayList();
		ArrayList<Double> polygonYA = new ArrayList();
		//根据车辆经纬度找到一个最近几个网点
		if (Utils.empty(ranges)) return true;
		for (String lnglat : ranges.split("\\|")) {
			if (Utils.empty(lnglat)) continue;
			polygonXA.add(Double.parseDouble(lnglat.split(",")[0]));
			polygonYA.add(Double.parseDouble(lnglat.split(",")[1]));
		}
		polygonXA.add(polygonXA.get(0));
		polygonYA.add(polygonYA.get(0));
		
		return isPointInPolygon(lng, lat, polygonXA, polygonYA);
	}
	
	public static boolean isIntersect(double px1, double py1, double px2, double py2, double px3, double py3, double px4, double py4)
	  {
	    boolean flag = false;
	    double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);
	    if (d != 0.0D) {
	      double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3)) / 
	        d;
	      double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1)) / 
	        d;
	      if ((r >= 0.0D) && (r <= 1.0D) && (s >= 0.0D) && (s <= 1.0D)) {
	        flag = true;
	      }
	    }
	    return flag;
	  }

	  public static boolean isPointOnLine(double px0, double py0, double px1, double py1, double px2, double py2)
	  {
	    boolean flag = false;
	    double ESP = 1.E-009D;
	    if ((Math.abs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP) && 
	      ((px0 - px1) * (px0 - px2) <= 0.0D) && 
	      ((py0 - py1) * (py0 - py2) <= 0.0D)) {
	      flag = true;
	    }
	    return flag;
	  }

	  public static double Multiply(double px0, double py0, double px1, double py1, double px2, double py2)
	  {
	    return (px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0);
	  }

	  public static boolean isPointInPolygon(double px, double py, ArrayList<Double> polygonXA, ArrayList<Double> polygonYA)
	  {
	    boolean isInside = false;
	    double ESP = 1.E-009D;
	    int count = 0;

	    double linePoint2x = 180.0D;

	    double linePoint1x = px;
	    double linePoint1y = py;
	    double linePoint2y = py;

	    for (int i = 0; i < polygonXA.size() - 1; i++) {
	      double cx1 = ((Double)polygonXA.get(i)).doubleValue();
	      double cy1 = ((Double)polygonYA.get(i)).doubleValue();
	      double cx2 = ((Double)polygonXA.get(i + 1)).doubleValue();
	      double cy2 = ((Double)polygonYA.get(i + 1)).doubleValue();

	      if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {
	        return true;
	      }

	      if (Math.abs(cy2 - cy1) < ESP)
	      {
	        continue;
	      }
	      if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x, 
	        linePoint2y))
	      {
	        if (cy1 > cy2) {
	          count++;
	        }
	      }
	      else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y, 
	        linePoint2x, linePoint2y))
	      {
	        if (cy2 > cy1)
	          count++;
	      }
	      else {
	        if (!isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y, 
	          linePoint2x, linePoint2y)) continue;
	        count++;
	      }
	    }
	    if (count % 2 == 1) {
	      isInside = true;
	    }

	    return isInside;
	  }
}